#!/usr/local/bin/python
# -*- coding: utf-8 -*-

import MySQLdb
import pymongo
import traceback
import redis
import config
import time

#------------------------------------------------------------------------------ 
class _redis:
    conn = None
    def __init__(self, i_host = 'localhost', i_port = 6379):
        try:
            _redis.conn.ping
        except:
            while True:
                try:
                    _redis.conn = redis.Redis(host = i_host, port = i_port)
                    _redis.conn.ping()
                    break
                except:
                    time.sleep(1)
                    traceback.print_exc()
#------------------------------------------------------------------------------ 
class _mongo():
    conn = None
    def __init__(self, i_host = 'localhost', i_port = 27017):
        if _mongo.conn == None:
            while True:
                try:
                    _mongo.conn = pymongo.MongoClient(i_host, i_port)
                    break
                except:
                    time.sleep(1)
                    traceback.print_exc()

#------------------------------------------------------------------------------ 
class _mysql():
    conn = None
    cursor = None
    def __init__(self):
        try:
            _mysql.conn.ping()
        except:
            self.connect()
    def connect(self):
        while True:
            try:
                _mysql.conn = MySQLdb.connect(host=config.MYSQL_HOST, port=config.MYSQL_PORT, user=config.MYSQL_USER, passwd=config.MYSQL_PASS, db=config.MYSQL_DB)
                _mysql.conn.ping()
                _mysql.cursor = _mysql.conn.cursor()
                break;
            except:
                traceback.print_exc()
                time.sleep(1)
    
    def close(self):
        try:
            _mysql.cursor.close()
        except:
            pass
        try:
            _mysql.conn.close()
        except:
            pass
    def refresh(self):
        try:
            _mysql.conn.ping()
        except:
            self.connect()
# #------------------------------------------------------------------------------ 
class cache(object,_mysql,_mongo, _redis):
    '''DB cache class'''
    _handlemysql = None
    _handmongodb = None
    _re_server = None
    _re_local = None
    def __init__(self):
#         self._handlemysql = _mysql()
#         if cache._handlemysql == None:
#             cache._handlemysql = Mysqldb()
        if cache._handmongodb == None:
            cache._handmongodb = _mongo()
        if cache._re_local == None: 
            cache._re_local = _redis()
        if cache._re_server == None: 
            cache._re_server = _redis()
             
#------------------------------------------------------------------------------ 
    def get_and_del(self, name, number):
        result = 0
        try:
#             redis.Redis.zremrangebyrank(name, 0, 1000)
            pipe = cache._re_local.re_lc.pipeline()
            pipe.multi()
            pipe.zrange(name, 0, number)
            pipe.zremrangebyrank(name, 0, number)
            res = pipe.execute()
            result = res[0]
        except:
            traceback.print_exc()
        return result
#------------------------------------------------------------------------------ 
# if __name__ == "__main__":
#     mongo = cache()._handmongodb.log
#     mongo.update({},{"$set": {"status": 3}})
    
#     a = cache()._handlemysql.mysql
#     results = 0
#     v_cursor = a.cursor()
#     v_cursor.callproc('get_next_task',())
#     results = v_cursor.fetchall()
#     v_cursor.close()
#     a.commit()
#     print results
